package com.academic_system.controller;

import com.academic_system.model.PartyActivity;
import com.academic_system.model.PartyActivityConditions;
import com.academic_system.service.PartyActivityService;
import com.academic_system.service.PartyMemberService;
import com.academic_system.service.PartyService;
import com.academic_system.util.DateUtil;
import com.academic_system.util.Message;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author 20155880 房雁颖
 * @description 党支部活动控制层
 * @project_name PartyActivityController
 * @package com.academic_system.controller
 * @create 2018-11-15 9:41
 **/
@Controller
@RequestMapping("/partyActivity")

@CrossOrigin(origins = "*")
public class PartyActivityController {
    @Autowired
    private PartyActivityService partyActivityService;
    @Autowired
    private PartyMemberService partyMemberService;
    @Autowired
    private PartyService partyService;

    /**
     * @param partyOrganizationNum
     * @param partyActivityName
     * @param partyActivityStartTime
     * @param partyActivityEndTime
     * @Description 党支部活动动态查询(按活动名称、活动时间)，活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityByConditions
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/queryPartyActivityByConditions",method = RequestMethod.GET)
    public Message queryPartyActivityByConditions(@Param("partyOrganizationNum")String partyOrganizationNum,@Param("partyActivityName")String partyActivityName,@Param("partyActivityStartTime")String partyActivityStartTime,@Param("partyActivityEndTime")String partyActivityEndTime){
        PartyActivityConditions partyActivityConditions=new PartyActivityConditions();
        System.out.println("start="+partyActivityStartTime);
        System.out.println("end="+partyActivityStartTime);
        if (partyActivityStartTime.equals("null")){
            partyActivityConditions.setPartyActivityStartTime(null);
        }else {
            Date StartTime= DateUtil.toDate(partyActivityStartTime);
            partyActivityConditions.setPartyActivityStartTime(StartTime);
        }

        if (partyActivityEndTime.equals("null")){
            partyActivityConditions.setPartyActivityEndTime(null);
        }else {
            Date EndTime= DateUtil.toDate(partyActivityEndTime);
            partyActivityConditions.setPartyActivityEndTime(EndTime);
        }
        partyActivityConditions.setPartyOrganizationNum(Integer.parseInt(partyOrganizationNum));
        partyActivityConditions.setPartyActivityName(partyActivityName);
        return partyActivityService.queryPartyActivityByConditions(partyActivityConditions);
    }

    /**
     * @param partyOrganizationNum
     * @param partyActivityName
     * @param partyActivityStartTime
     * @param partyActivityEndTime
     * @Description 党支部活动动态查询(按活动名称、活动时间)，活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityByConditions
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/queryPartyActivityByConditionsByCache",method = RequestMethod.GET)
    public Message queryPartyActivityByConditionsByCache(@Param("partyOrganizationNum")String partyOrganizationNum,@Param("partyActivityName")String partyActivityName,@Param("partyActivityStartTime")String partyActivityStartTime,@Param("partyActivityEndTime")String partyActivityEndTime){
        PartyActivityConditions partyActivityConditions=new PartyActivityConditions();
        System.out.println("start="+partyActivityStartTime);
        System.out.println("end="+partyActivityStartTime);
        if (partyActivityStartTime.equals("null")){
            partyActivityConditions.setPartyActivityStartTime(null);
        }else {
            Date StartTime= DateUtil.toDate(partyActivityStartTime);
            partyActivityConditions.setPartyActivityStartTime(StartTime);
        }

        if (partyActivityEndTime.equals("null")){
            partyActivityConditions.setPartyActivityEndTime(null);
        }else {
            Date EndTime= DateUtil.toDate(partyActivityEndTime);
            partyActivityConditions.setPartyActivityEndTime(EndTime);
        }
        partyActivityConditions.setPartyOrganizationNum(Integer.parseInt(partyOrganizationNum));
        partyActivityConditions.setPartyActivityName(partyActivityName);
        return partyActivityService.queryPartyActivityByConditionsByCache(partyActivityConditions);
    }
//    /**
//     * @param partyOrganizationNum
//     * @param partyActivityName
//     * @param partyActivityStartTime
//     * @param partyActivityEndTime
//     * @Description 党支部活动动态查询(按活动名称、活动时间),临时表
//     * @author 20155880 房雁颖
//     * @methodName queryPartyActivityByConditions
//     * @returnType Message
//     * @create 2018/11/15 8:45
//     **/
//    @RequestMapping(value = "/queryPartyActivityTempByConditions",method = RequestMethod.GET)
//    public Message queryPartyActivityTempByConditions(@Param("partyOrganizationNum")String partyOrganizationNum,@Param("partyActivityName")String partyActivityName,@Param("partyActivityStartTime")String partyActivityStartTime,@Param("partyActivityEndTime")String partyActivityEndTime){
//        PartyActivityConditions partyActivityConditions=new PartyActivityConditions();
//        System.out.println("start="+partyActivityStartTime);
//        System.out.println("end="+partyActivityStartTime);
//        if (partyActivityStartTime.equals("null")){
//            partyActivityConditions.setPartyActivityStartTime(null);
//        }else {
//            Date StartTime= DateUtil.toDate(partyActivityStartTime);
//            partyActivityConditions.setPartyActivityStartTime(StartTime);
//        }
//
//        if (partyActivityEndTime.equals("null")){
//            partyActivityConditions.setPartyActivityEndTime(null);
//        }else {
//            Date EndTime= DateUtil.toDate(partyActivityEndTime);
//            partyActivityConditions.setPartyActivityEndTime(EndTime);
//        }
//        partyActivityConditions.setPartyOrganizationNum(Integer.parseInt(partyOrganizationNum));
//        partyActivityConditions.setPartyActivityName(partyActivityName);
//        return partyActivityService.queryPartyActivityTempByConditions(partyActivityConditions);
//    }

    /**
     * @param partyMemberAccount
     * @Description 根据老师编号/学生学号查询党支部名称
     * @author 20155880 房雁颖
     * @methodName queryPartyNameAndPON
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/queryPartyNameAndPON",method = RequestMethod.GET)
    public Message queryPartyNameAndPON(@Param("partyMemberAccount")int partyMemberAccount){
        int partyOrganizationNum=partyMemberService.findPONBypartyMemberAccount(partyMemberAccount);
        System.out.println("partyOrganizationNum="+partyOrganizationNum);
        String partyName=partyService.queryPartyNameByPartyOrganizationNum(partyOrganizationNum);
        System.out.println("partyName="+partyName);
        Message message=new Message();
        message.add("partyName",partyName);
        message.add("PON",partyOrganizationNum);
        return message;
    }

    /**
     * @param partyActivityId
     * @Description 通过党支部活动ID查找党支部活动信息,活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityById
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/queryPartyActivityById",method = RequestMethod.GET)
    public Message queryPartyActivityById(@Param("partyActivityId")int partyActivityId){
        return partyActivityService.queryPartyActivityById(partyActivityId);
    }

//    /**
//     * @param partyActivityId
//     * @Description 通过党支部活动ID查找党支部活动信息，临时表
//     * @author 20155880 房雁颖
//     * @methodName queryPartyActivityTempById
//     * @returnType Message
//     * @create 2018/11/15 8:45
//     **/
//    @RequestMapping(value = "/queryPartyActivityTempById",method = RequestMethod.GET)
//    public Message queryPartyActivityTempById(@Param("partyActivityId")int partyActivityId){
//        return partyActivityService.queryPartyActivityTempById(partyActivityId);
//    }


    /**
     * @param partyActivity
     * @Description 保存
     * @author 20155880 房雁颖
     * @methodName savePartyActivity
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/savePartyActivity",method = RequestMethod.POST)
    public Message savePartyActivity(@RequestBody PartyActivity partyActivity){
        return partyActivityService.savePartyActivity(partyActivity);
    }

    /**
     * @param partyActivity
     * @Description 提交
     * @author 20155880 房雁颖
     * @methodName commitPartyActivity
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/commitPartyActivity",method = RequestMethod.POST)
    public Message commitPartyActivity(@RequestBody PartyActivity partyActivity){
        return partyActivityService.commitPartyActivity(partyActivity);
    }

    /**
     * @param partyActivityId
     * @Description 党支部活动删除
     * @author 20155880 房雁颖
     * @methodName deletePartyActivity
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/deletePartyActivity",method = RequestMethod.GET)
    public Message deletePartyActivity(@Param("partyActivityId")int partyActivityId){
        return partyActivityService.deletePartyActivity(partyActivityId);
    }

    /**
     * @param partyActivityId
     * @Description 通过党支部活动ID查找党支部名称
     * @author 20155880 房雁颖
     * @methodName queryPartyNameByPAI
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/queryPartyNameByPAI",method = RequestMethod.GET)
    public Message queryPartyNameByPAI(@Param("partyActivityId")int partyActivityId){
        return partyActivityService.queryPartyNameByPAI(partyActivityId);
    }

    /**
     * @param partyActivity
     * @Description 党支部活动录入
     * @author 20155880 房雁颖
     * @methodName addPartyActivity
     * @returnType Message
     * @create 2018/11/15 8:45
     **/
    @RequestMapping(value = "/addPartyActivity",method = RequestMethod.POST)
    public Message addPartyActivity(@RequestBody PartyActivity partyActivity){
        return partyActivityService.addPartyActivity(partyActivity);
    }

    /**
     * @param request
     * @Description 批量删除
     * @author 20155880 房雁颖
     * @methodName deleteMore
     * @returnType Message
     * @create 2018/11/15   8:45
     **/
    @RequestMapping(value = "/deleteMore", method = RequestMethod.POST)
    public Message deleteMore(HttpServletRequest request, HttpServletResponse response) {
        String items = request.getParameter("delItems");
        System.out.println("----items---"+items);
        Message message=new Message();
        String[] item = items.split(",");
        for (int i = 0; i < item.length; i++) {
            System.out.println("-------"+Integer.parseInt(item[i]));
            message= partyActivityService.deletePartyActivity(Integer.parseInt(item[i]));
        }
        return message;
    }
}
